package services

import (
	"com.opennews.openplatform/web_api/domain"
	"com.opennews.openplatform/web_api/shared"
)

type ProductService interface {
	List() []shared.MapData
}

func NewProductService() ProductService {
	return productService{}
}

func (this productService) List() []shared.MapData {
	var products []domain.Product

	shared.DB.Model(&domain.Product{}).Offset(10000).Limit(10).Find(&products)

	result := []shared.MapData{}

	for i := range products {
		result = append(result, this.toMap(products[i]))
	}

	return result
}

// Converts user model to map[string]interface{} with needed properties for api client.
func (this productService) toMap(product domain.Product) shared.MapData {
	return shared.MapData{
		"id":   product.ID,
		"name": product.Name,
	}
}

type productService struct{}
